package org.hgvc.webapp.ets.jsf.beans.mastersetup;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;

import org.hgvc.webapp.ets.bo.Country;
import org.hgvc.webapp.ets.constant.AdminConstant;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.spring.service.MasterSetupService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;


public class CountryBean extends AbstractBean{
	public static final String BEAN_NAME="countryBean";
	
	private MasterSetupService masterSetupService;
	
	private List<Country> countryList;
	private Country country;
	private boolean catchError;
	
	public List<Country> getCountryList() {
		return countryList;
	}
	public void setCountryList(List<Country> countryList) {
		this.countryList = countryList;
	}
	public Country getCountry() {
		return country;
	}
	public void setCountry(Country country) {
		this.country = country;
	}
	public void setMasterSetupService(MasterSetupService masterSetupService) {
		this.masterSetupService = masterSetupService;
	}
	
	public void init(){
		try{
			countryList = masterSetupService.getCountryList();	
		}catch(BaseException be){
			initErrorBean(be);
		}
	}
	
	public String navAddAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		outcome = outcome.concat("_"+getRoleName());
		country=new Country();
		return outcome;
	}
	public String addAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		outcome = outcome.concat("_"+getRoleName());
		if (catchError)
			outcome=null;
		return outcome;
	}
	public void addActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			this.country.setCreatedBy(getThisUser());
			this.country.setCreatedDate(new Date());
			this.country.setIsDeleted(CommonConstant.NOT_DELETED);
			this.masterSetupService.saveCountry(this.country);
			this.countryList = this.masterSetupService.getCountryList();
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	
	public String navEditAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		outcome = outcome.concat("_"+getRoleName());
		if (catchError)
			outcome=null;
		return outcome;
	}
	public void navEditActionEvent(ActionEvent event){
		this.catchError=false;
		UIParameter param = (UIParameter) event.getComponent().findComponent("countryId");
		String countryId = (String)param.getValue();
		for(Country country:countryList){
			if(countryId.equalsIgnoreCase(country.getCountryId())){
				this.country = country;
				break;
			}
		}
	}
	
	public String updateAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		outcome = outcome.concat("_"+getRoleName());
		if (catchError)
			outcome=null;
		return outcome;
	}
	public void updateActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			this.country.setModifiedBy(getThisUser());
			this.country.setModifiedDate(new Date());
			this.masterSetupService.updateCountry(this.country);
			this.countryList = this.masterSetupService.getCountryList();
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	
	public String deleteAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		outcome = outcome.concat("_"+getRoleName());
		if (catchError)
			outcome=null;
		return outcome;
	}
	public void deleteActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			List<Country> deleteList = new ArrayList<Country>();
			for (Country country : this.countryList){
				if (country.isMarkAsAction())
					deleteList.add(country);
			}
			this.masterSetupService.deleteCountries(deleteList);
			this.countryList = this.masterSetupService.getCountryList();
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	
	public String backToListAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		outcome = outcome.concat("_"+getRoleName());
		return outcome;
	}
}
